﻿using RXY_Mall.Application.Order;
using RXY_Mall.Application.Order.Dto;
using RXY_Mall.Application.Return;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace RXY_Mall.Shop.Controllers
{
    public class ReturnController : BaseController
    {
        private IReturnService returnService;
        private IOrderService orderService;

        public ReturnController()
        {
            returnService = new ReturnService();
            orderService = new OrderService();
        }

        #region 退换货
        [HttpGet]
        public ActionResult ReturnOrders()
        {
            return View();
        }

        [HttpGet]
        public JsonResult GetReturns(string sEcho, int iDisplayStart, int iDisplayLength, string return_no, string contact_mobile, string shop_name, string start_date, string end_date, int return_status = 0, int return_type = 0, int return_reason = 0)
        {
            var result = returnService.GetReturns(return_no, contact_mobile, shop_name, return_type, return_reason, start_date, end_date, return_status, iDisplayStart / iDisplayLength + 1, iDisplayLength, ShopId);
            if (result.code != 1)
            {
                ErrorNotification(result.msg);
            }
            return Json(new { sEcho = sEcho, iTotalRecords = result.data.TotalCount, iTotalDisplayRecords = result.data.TotalCount, aaData = result.data }, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public async Task<ActionResult> ReturnOrder(int id)
        {
            var result = await returnService.GetReturn(id, ShopId);
            if (result.code != 1)
            {
                ErrorNotification(result.msg);
            }
            var result2 = orderService.GetLogistics();
            ViewBag.Logistics = result2.data;
            return View(result.data);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> UpdateReturnAudit(UpdateReturnAuditInput input)
        {
            input.shop_id = ShopId;
            if (ModelState.IsValid)
            {
                var result = await returnService.UpdateReturnAudit(input);
                if (result.code != 1)
                {
                    ErrorNotification(result.msg);
                }
                else
                {
                    SuccessNotification(result.msg);
                }
            }
            return RedirectToAction("ReturnOrder", new { id = input.return_id });
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> UpdateReturnRefund(UpdateReturnRefundInput input)
        {
            input.shop_id = ShopId;
            if (ModelState.IsValid)
            {
                var result = await returnService.UpdateReturnRefund(input);
                if (result.code != 1)
                {
                    ErrorNotification(result.msg);
                }
                else
                {
                    SuccessNotification(result.msg);
                }
            }
            return RedirectToAction("ReturnOrder", new { id = input.return_id });
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> UpdateReturnShipment(UpdateReturnShipmentInput input)
        {
            input.shop_id = ShopId;
            if (ModelState.IsValid)
            {
                var result = await returnService.UpdateReturnShipment(input);
                if (result.code != 1)
                {
                    ErrorNotification(result.msg);
                }
                else
                {
                    SuccessNotification(result.msg);
                }
            }
            return RedirectToAction("ReturnOrder", new { id = input.return_id });
        }
        #endregion
    }
}